using System;
using System.Collections.Generic;
using System.Text;
using MyMath;

namespace Physics
{
    public abstract class ConstrainingObject
    {
        public abstract bool IntersectSphere(MyVector position, MyVector velocity, CollisionSphere sphere, out MyVector intersectionPoint, out MyVector intersectionNormal,out MyVector translateBack);
        public abstract bool IntersectPoint(MyVector position, MyVector velocity, out MyVector intersectionPoint, out MyVector intersectionNormal,out MyVector translateBack);
        public bool IntersectMesh(MyVector position, MyVector velocity, CollisionMesh mesh, out MyVector intersectionPoint, out MyVector intersectionNormal,out MyVector translateBack)
        {
            intersectionPoint = new MyVector();
            intersectionNormal = new MyVector();
            translateBack=new MyVector();
            MyVector translation=new MyVector(0,0,0);

            MyVector tempIP,tempIN;
            int nIP = 0;
            for (int i = 0; i < mesh.m_hardPoints.Length; i++)
            {
                MyVector vertVel = mesh.m_hardPoints[i] ^ mesh.MeshRotation;
                if(IntersectPoint(mesh.m_hardPoints[i]+position,velocity/*+vertVel*/,out tempIP,out tempIN,out translation))
                {
                    intersectionPoint += tempIP;
                    nIP++;
                    intersectionNormal += tempIN;
                    translateBack += translation;
                }
            }
            if (nIP > 1)
            {
                intersectionPoint.Divide(nIP);
                intersectionNormal.Normalize();
                translateBack.Divide(nIP);
            }

            return nIP>0;
        }

        public IPhysicalObject PhysicalObject;


    }
}
